var RiskAssessor = require("./RiskAssessor")
var express = require('express');
var config = require("./util/config")

var app = express();
let healthRouter =express.Router();
let collapseRouter = express.Router();
let hydroRouter = express.Router();
let deformationRouter = express.Router();
let riskRouter = express.Router();

const ConsulConfig = require('./ConsulConfig');
const consul = new ConsulConfig();
app.use('/api/Health', healthRouter);

//添加两个路由到应用上
app.use('/api/analysis/collapserisk', collapseRouter);
app.use('/api/analysis/hydrologyrisk', hydroRouter);
app.use('/api/analysis/risk', riskRouter);
app.use('/api/analysis/deformationrisk', deformationRouter)

healthRouter.get('', function (req, res1) {
    res1.end('OK');
})
//突涌水风险
async function getHydrologyRisk(mile) {
    mile = mile ? mile : "ZK5800"
    var riskAssessor = new RiskAssessor()
    var result = await riskAssessor.HydrologyRiskAssess(mile)
    //console.log(result)
    return result
}
//塌方风险
async function getCollapseRisk(mile) {
    mile = mile ? mile : "ZK5800"
    var riskAssessor = new RiskAssessor()
    var result = await riskAssessor.CollapseRiskAssess(mile)
    //console.log(result)
    return result
}
/**
 * 大变形风险
 * @param {*} mile 
 */
async function getDeformationRisk(mile) {
    mile = mile ? mile : "ZK5800"
    var riskAssessor = new RiskAssessor()
    var result = await riskAssessor.DeformationAssess(mile)
    //console.log(result)
    return result
}
async function getCollapseRisk_section(section) {
    section = section ? section : "S2"
    var riskAssessor = new RiskAssessor()
    var result = await riskAssessor.CollapseRiskAssess_section(section)
    //console.log(result)
    return result
}
//所有风险
async function getAllRisk(mile) {
    mile = mile ? mile : "ZK5800"
    var riskAssessor = new RiskAssessor()
    var result = await riskAssessor.loadAllRisk(mile)
    //console.log(result)
    return result
}
//所有风险--按照标段
async function getAllRisk_section(section) {
    mile = mile ? mile : "ZK5800"
    var riskAssessor = new RiskAssessor()
    var result = await riskAssessor.loadAllRisk(mile)
    //console.log(result)
    return result
}
//所有风险-按里程
riskRouter.get('/mile/:mile', function (req, res1) {
    //req:请求头，包含请求参数等等
    getAllRisk(req.params.mile).then(res => {
        res1.writeHead(200, {
            'Content-Type': 'text/html;charset=utf-8'
        })
        var result = JSON.stringify(res)
        res1.end(result)
    })
})
//
riskRouter.get('/section/:section', function (req, res1) {
    //req:请求头，包含请求参数等等
    getAllRisk_section(req.params.section).then(res => {
        res1.writeHead(200, {
            'Content-Type': 'text/html;charset=utf-8'
        })
        var result = JSON.stringify(res)
        res1.end(result)
    })
})
//突涌水风险，按里程
hydroRouter.get('/mile/:mile', function (req, res1) {
    //req:请求头，包含请求参数等等
    getHydrologyRisk(req.params.mile).then(res => {
        res1.writeHead(200, {
            'Content-Type': 'text/html;charset=utf-8'
        })
        var result = JSON.stringify(res)
        res1.end(result)
    })
})
//突涌水风险 
hydroRouter.get('/', function (req, res1) {
    getHydrologyRisk(req.params.mile).then(res => {
        res1.writeHead(200, {
            'Content-Type': 'text/html;charset=utf-8'
        })
        var result = JSON.stringify(res)
        res1.end(result)
    })
})
//塌方风险,按里程 /api/anasys/collapserisk/mile/ZK5800
collapseRouter.get('/mile/:mile', function (req, res1) {
    getCollapseRisk(req.params.mile).then(res => {
        res1.writeHead(200, {
            'Content-Type': 'text/html;charset=utf-8'
        })
        var result = JSON.stringify(res)
        res1.end(result)
    })
})
//塌方风险，默认为当前最新位置的风险
collapseRouter.get('/section/:section', function (req, res1) {
    getCollapseRisk_section(req.params.section).then(res => {
        res1.writeHead(200, {
            'Content-Type': 'text/html;charset=utf-8'
        })
        var result = JSON.stringify(res)
        res1.end(result)
    })
})
//大变形风险
deformationRouter.get('/mile/:mile', function (req, res1) {
    deformationRouter(req.params.mile).then(res => {
        res1.writeHead(200, {
            'Content-Type': 'text/html;charset=utf-8'
        })
        var result = JSON.stringify(res)
        res1.end(result)
    })
})
//express 客户端，用于监听用户请求
var server = app.listen(config.RiskPort, function () {
    var host = server.address().address
    var port = server.address().port
    console.log("应用实例，访问地址为 http://%s:%s", host, port)
})